package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * 体检套餐管理
 *
 * @create 2020/11/19 0019 10:30
 */

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    //使用redisPool操作redis服务
    @Autowired
    private JedisPool jedisPool;
    
    @Reference
    private SetmealService setmealService;
    
    //文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        //获取图片原始文件名
        String originalFilename = imgFile.getOriginalFilename();
        String extion = originalFilename.substring(originalFilename.lastIndexOf(".") - 1);
        String fileName = UUID.randomUUID().toString() + extion;
        try {
            //将文件上传到七牛云服务器
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
    }
    
    //新增套餐
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.add(setmeal, checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }
    
    @PostMapping("/findPage")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return setmealService.pageQuery(queryPageBean);
    }
    
    //根据套餐ID查询该套餐所有信息
    @RequestMapping("/findBySetmealId")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    public Result findBySetmealId(Integer setmealId) {
        try {
            Setmeal setmeal = setmealService.findById(setmealId);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }
    
    //根据套餐ID回显勾选的检查组
    @PostMapping("/findcheckgroupIdsBySetmealId")
    @PreAuthorize("hasAuthority('SETMEAL_QUERY')")
    public Result findcheckgroupIdsBySetmealId(Integer setmealId) {
        try {
            List<Integer> list = setmealService.findcheckgroupIdsBySetmealId(setmealId);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }
    
    //编辑套餐
    @RequestMapping("/edit")   // @RequestBody 表明参数为json类型
    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkgroupIds) {
        try {
            setmealService.edit(setmeal, checkgroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }
    
    //删除套餐
    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    public Result delete(Integer SetmealId) {
        try {
            setmealService.delete(SetmealId);

            /*Setmeal setmeal = setmealService.findById(SetmealId);
            String setmealDb = JSON.toJSONString(setmeal);
            //redis中拿到对应的套餐信息
            List<String> setmealList = jedisPool.getResource().lrange("setmeal",0,-1);
            for (String jsonString : setmealList) {
                //判断从数据库中拿到的套餐信息在redis中是否存在，如果存在就删除。
                if (jsonString.equals(setmealDb)){
                    jedisPool.getResource().lrem("setmeal",0,setmealDb);
                }
            }*/


        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
}
